package com.dgcsxy.my_college_note.controller;


import com.dgcsxy.my_college_note.common.utils.ResponseResult;
import com.dgcsxy.my_college_note.entity.dto.user.UserInfoDto;
import com.dgcsxy.my_college_note.entity.vo.user.UserLoginVo;
import com.dgcsxy.my_college_note.entity.vo.user.UserRegisterVo;
import com.dgcsxy.my_college_note.entity.vo.user.UserUpdateVo;
import com.dgcsxy.my_college_note.service.McnUserService;
import com.dgcsxy.my_college_note.service.SendEmailService;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Yjx
 * @since 2022-10-24
 */
@RestController
@RequestMapping("/api/user")
@CrossOrigin
public class McnUserController {

    private final McnUserService mcnUserService;
    private final SendEmailService sendEmailService;

    public McnUserController(McnUserService mcnUserService, SendEmailService sendEmailService) {
        this.mcnUserService = mcnUserService;
        this.sendEmailService = sendEmailService;
    }

    @PostMapping("/send/{email}")
    public ResponseResult sendEmail(@PathVariable("email") String email) {
        sendEmailService.sendEmailCaptcha(email);
        return ResponseResult.SUCCESS();
    }

    @PostMapping("/register")
    public ResponseResult register(@RequestBody UserRegisterVo userRegisterDto) {
        String message = mcnUserService.register(userRegisterDto);
        return ResponseResult.SUCCESS(message);
    }

    @PostMapping("/login")
    public ResponseResult login(@RequestBody UserLoginVo userLoginVo) {
        String message = mcnUserService.login(userLoginVo);
        return ResponseResult.SUCCESS(message);
    }

    @PutMapping("/update")
    public ResponseResult updateUser(@RequestBody UserUpdateVo userUpdateDto) {
        String message = mcnUserService.updateUser(userUpdateDto);
        return ResponseResult.SUCCESS(message);
    }

    @GetMapping("/get/{userEmail}")
    public ResponseResult selectUserById(@PathVariable("userEmail") String userEmail) {
        UserInfoDto userInfoDto = mcnUserService.selectUserByEmail(userEmail);
        return userInfoDto != null ? ResponseResult.SUCCESS(userInfoDto)
                : ResponseResult.FAILED("查无此用户");
    }

}

